var UserNameSelector = {
	/**
	 * e
	 * d 表示多选人 或者单选 默认多选
	 * b 电话号码
	 */
	getView : function(e, d, f, b) {
		if (typeof (e) == "object") {
			this.scope = e.scope;
			this.callback = e.callback;
		} else {
			this.scope = this;
			this.callback = e;
		}
		this.isSingle = (d != null) ? d : true;
		this.mobileFlag = (b != null) ? b : false;
		var a = this.initPanel(d);
		var c = new Ext.Window({
			id : "UserSelectorWin",
			title : "选择用户",
			iconCls : "menu-appuser",
			width : 540,
			minWidth : 540,
			height : 380,
			minHeight : 380,
			layout : "fit",
			border : false,
			maximizable : true,
			resizable : true,
			modal : true,
			items : [ a ],
			buttonAlign : "center",
			buttons : [ {
				text : "确认",
				iconCls : "btn-ok",
				scope : this,
				handler : this.submit
			}, {
				text : "关闭",
				iconCls : "btn-cancel",
				scope : this,
				handler : this.close
			} ]
		});
		if (f) {
			c.addButton(new Ext.Button({
				text : "发起人",
				iconCls : "menu-subuser",
				scope : this,
				handler : function() {
					this.callback.call(this, "__start", "[发起人]");
					c.close();
				}
			}));
		}
		return c;
	},
	initPanel : function(g) {
		var k = new Ext.data.Store({
			proxy : new Ext.data.HttpProxy({
				url : appConfig.contPath + "/account/user!searchChooseByDepId.action"
			}),
			reader : new Ext.data.JsonReader({
				root : "results",
				totalProperty : "totalCounts",
				id : "id",
				fields : [ {
					name : "id"
				}, "name", "userName", "mobilePhone" ,"email","sex"]
			}),
			remoteSort : true
		});
		k.setDefaultSort("userName", "desc");
		k.load({
			params : {
				start : 0,
				limit : 12
			}
		});
		var e = null;
		if (g) {
			e = new Ext.grid.CheckboxSelectionModel({
				singleSelect : true
			});
		} else {
			e = new Ext.grid.CheckboxSelectionModel();
		}
		var l = new Ext.grid.ColumnModel(
				{
					columns : [
							e,
							new Ext.grid.RowNumberer(),
							{
								header : "用户名",
								dataIndex : "name",
								renderer : function(p, q, o) {
									var r = o.data.sex;
									if (r == '2') {
										return '<img src="'
												+ appConfig.contPath
												+ '/ext/resources/css/glimages/flag/women.png"/>&nbsp;'
												+ p;
									} else {
										return '<img src="'
												+ appConfig.contPath
												+ '/ext/resources/css/glimages/flag/man.png"/>&nbsp;'
												+ p;
									}
								},
								width : 60
							} ],
					defaults : {
						sortable : true,
						menuDisabled : true,
						width : 120
					},
					listeners : {
						hiddenchange : function(o, p, q) {
							saveConfig(p, q);
						}
					}
				});
		var f = new Ext.tree.TreePanel({
			id : "treePanels",
			autoScroll : true,
			title : "按部门分类 ",
			iconCls : "dep-user",
			loader : new Ext.tree.TreeLoader({
				url : appConfig.contPath + "/account/json/inside_organization!searchForTree.action"
			}),
			root : new Ext.tree.AsyncTreeNode({
				expanded : true
			}),
			rootVisible : false, 
			listeners : {
				click : this.clickNode
			}
		});
		var n = new Ext.tree.TreePanel({
			id : "rolePanel",
			autoScroll : true,
			iconCls : "role-user",
			title : "按角色分类 ",
			loader : new Ext.tree.TreeLoader({
				url : appConfig.contPath + "/account/json/role!searchForTree.action"
			}),
			root : new Ext.tree.AsyncTreeNode({
				expanded : true
			}),
			rootVisible : false,
			listeners : {
				"click" : this.clickRoleNode
			}
		});
		var jdu = new Ext.tree.TreePanel({
			id : "jduPanel",
			autoScroll : true,
			iconCls : "role-user",
			title : "按监督组分类 ",
			loader : new Ext.tree.TreeLoader({
				url : appConfig.contPath +"/account/json/supervise!searchForTree.action"
			}),
			root : new Ext.tree.AsyncTreeNode({
				expanded : true
			}),
			rootVisible : false,
			listeners : {
				"click" : this.clickjduNode
			}
		});
		var h = new Ext.Panel({
			id : "onlinePanel",
			autoScroll : true,
			iconCls : "online-user",
			title : "在线人员  ",
			listeners : {
				"expand" : this.clickOnlinePanel
			}
		});
		var m = new Ext.grid.EditorGridPanel({
			title : "用户列表",
			autoScroll : true,
			id : "contactGrid",
			region : "center",
			height : 380,
			autoWidth : false,
			store : k,
			shim : true,
			trackMouseOver : true,
			disableSelection : false,
			loadMask : true,
			cm : l,
			sm : e,
			viewConfig : {
				forceFit : true,
				enableRowBody : false,
				showPreview : false
			}
		});
		m.on("rowdblclick", function(o, w, v) {
			var x = Ext.getCmp("contactGrid");
			var p = Ext.getCmp("selectedUserGrid");
			var y = p.getStore();
			var B = x.getSelectionModel().getSelections();
			for ( var r = 0; r < B.length; r++) {
				var s = B[r].data.userName;
				var z = B[r].data.name;
				var u = false;
				for ( var q = 0; q < y.getCount(); q++) {
					if (y.getAt(q).data.userName == s) {
						u = true;
						break;
					}
				}
				if (!u) {
					var A = {
						userName : s,
						name : z
					};
					var t = new y.recordType(A);
					p.stopEditing();
					y.add(t);
				}
			}
		});
		var d = new Ext.FormPanel({
			id : "userSelectorSearchPanel",
			height : 38,
			region : "north",
			layout : "hbox",
			bodyStyle : "padding:6px 2px 2px 2px",
			layoutConfigs : {
				align : "middle"
			},
			keys : {
				key : Ext.EventObject.ENTER,
				scope : this,
				fn : this.search
			},
			defaultType : "label",
			defaults : {
				margins : "0 0 0 4"
			},
			items : [ {
				text : "用户姓名"
			}, {
				xtype : "textfield",
				name : "filter_LIKES_name",
				width : 260,
				maxLength : 256
			}, {
				xtype : "button",
				text : "查询",
				iconCls : "btn-search",
				
				scope : this,
				handler : this.search
			} ]
		});
		var j = new Ext.grid.CheckboxSelectionModel();
		var c = new Ext.grid.EditorGridPanel({
			id : "selectedUserGrid",
			title : "已选用户",
			layout : "form",
			region : "center",
			width : "100%",
			autoWidth : true,
			height : "100%",
			autoHeight : true,
			autoScroll : true,
			border : false,
			store : new Ext.data.ArrayStore({
				fields : [ "userId", "fullname" ]
			}),
			trackMouseOver : true,
			sm : j,
			columns : [ j, new Ext.grid.RowNumberer(), {
				header : "用户名",
				dataIndex : "name"
			} ]
		});
		c.addListener("rowdblclick", function(q, s) {
			var q = Ext.getCmp("selectedUserGrid");
			var o = q.getStore();
			var r = q.getSelectionModel().getSelections();
			for ( var p = 0; p < r.length; p++) {
				q.stopEditing();
				o.remove(r[p]);
			}
		});
		var i = new Ext.Panel({
			layout : "border",
			region : "east",
			width : 200,
			height : "100%",
			border : false,
			autoScroll : true,
			items : [ new Ext.Panel({
				region : "west",
				frame : true,
				width : 40,
				layout : {
					type : "vbox",
					pack : "center",
					align : "stretch"
				},
				defaultType : "button",
				items : [ {
					iconCls : "add-all",
					text : "",
					scope : this,
					handler : this.addAll
				}, {
					iconCls : "rem-all",
					text : "",
					scope : this,
					handler : this.removeAll
				} ]
			}), {
				region : "center",
				autoScroll : true,
				items : [ c ]
			} ]
		});
		var b = new Ext.Panel({
			layout : "accordion",
			region : "west",
			width : 160,
			split : true,
			header : false,
			collapsible : true,
			items : [ f, n, jdu,h ]
		});
		var a = new Ext.Panel({
			id : "contactPanel",
			layout : "border",
			region : "center",
			border : false,
			anchor : "96%,96%",
			items : [ d, b, m ]
		});
		if (g != null && g == false) {
			a.add(i);
			a.doLayout();
		}
		return a;
	},
	
	clickNode : function(b) {
		if (b != null) {
			var c = Ext.getCmp("contactGrid");
			var a = c.getStore();
			a.proxy.conn.url = appConfig.contPath + "/account/user!searchChooseByDepId.action";
			a.load({
				params : {
					orgId : b.id
				}
			});
		}
	},
	clickRoleNode : function(b) {
		if (b != null) {
			var c = Ext.getCmp("contactGrid");
			var a = c.getStore();
			a.baseParams = {
				roleId : b.id
			};
			a.proxy.conn.url = appConfig.contPath + "/account/user!searchChooseByRole.action";
			a.load({
				params : {
					roleId:b.id
				}
			});
		}
	},
	clickjduNode : function(b) {
		if (b != null) {
			var c = Ext.getCmp("contactGrid");
			var a = c.getStore();
			a.proxy.conn.url = appConfig.contPath + "/app/supervisor!searchChooseBySeId.action";
			a.load({
				params : {
					sperviseId:b.id
				}
			});
		}
	},
	clickOnlinePanel : function() {
		var b = Ext.getCmp("contactGrid");
		var a = b.getStore();
		a.proxy.conn.url = appConfig.contPath + "/system/onlineAppUser.do";
		a.load({
			params : {
				start : 0,
				limit : 200
			}
		});
	},
	addAll : function() {
		var g = Ext.getCmp("contactGrid");
		var a = Ext.getCmp("selectedUserGrid");
		var h = a.getStore();
		var m = g.getSelectionModel().getSelections();
		for ( var c = 0; c < m.length; c++) {
			var d = m[c].data.userName;
			var k = m[c].data.name;
			var f = false;
			for ( var b = 0; b < h.getCount(); b++) {
				if (h.getAt(b).data.userName == d) {
					f = true;
					break;
				}
			}
			if (!f) {
				var l = {
					userName : d,
					name : k
				};
				var e = new h.recordType(l);
				a.stopEditing();
				h.add(e);
			}
		}
	},
	removeAll : function() {
		var c = Ext.getCmp("selectedUserGrid");
		var d = c.getSelectionModel().getSelections();
		var a = c.getStore();
		for ( var b = 0; b < d.length; b++) {
			c.stopEditing();
			a.remove(d[b]);
		}
	},
	search : function() {
		var a = Ext.getCmp("userSelectorSearchPanel");
		var b = Ext.getCmp("contactGrid");
		a.getForm().submit(
				{
					url : appConfig.contPath + "/account/user!searchChooseByUserName.action",
					method : "post",
					success : function(d, e) {
						b.getStore().proxy.conn.url = appConfig.contPath
								+ "/account/user!searchChooseByUserName.action";
						var c = Ext.util.JSON.decode(e.response.responseText);
						b.getStore().loadData(c);
					}
				});
	},
	submit : function() {
		var f = "";
		var d = "";
		if (this.isSingle == null || this.isSingle) {
			var c = Ext.getCmp("contactGrid");
			var e = c.getSelectionModel().getSelections();
			for ( var b = 0; b < e.length; b++) {
				if (b > 0) {
					f += ",";
					d += ",";
				}
				f += e[b].data.userName;
				if (this.mobileFlag) {
					d += e[b].data.name + "(" + e[b].data.mobilePhone + ")";
				} else {
					d += e[b].data.name;
				}
			}
		} else {
			var a = Ext.getCmp("selectedUserGrid").getStore();
			for ( var b = 0; b < a.getCount(); b++) {
				if (b > 0) {
					f += ",";
					d += ",";
				}
				f += a.getAt(b).data.userName;
				if (this.mobileFlag) {
					d += a.getAt(b).data.name + "("
							+ a.getAt(b).data.mobilePhone + ")";
				} else {
					d += a.getAt(b).data.name;
				}
			}
		}
		if (this.callback != null) {
			this.callback.call(this.scope, f, d);
		}
		Ext.getCmp("UserSelectorWin").close();
	},
	close : function() {
		Ext.getCmp("UserSelectorWin").close();
	}
};